package com.ruoyi.site.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.site.domain.Tracks;
import com.ruoyi.site.service.ITracksService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.site.domain.Genres;
import com.ruoyi.site.service.IGenresService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 音乐流派Controller
 * 
 * @author chuckeytang
 * @date 2025-08-17
 */
@RestController
@RequestMapping("/site/genres")
public class GenresController extends BaseController
{
    @Autowired
    private IGenresService genresService;

    @Autowired
    private ITracksService tracksService;

    /**
     * 查询音乐流派列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Genres genres)
    {
        startPage();
        List<Genres> list = genresService.selectGenresList(genres);
        return getDataTable(list);
    }

    /**
     * 导出音乐流派列表
     */
    @Log(title = "音乐流派", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Genres genres)
    {
        List<Genres> list = genresService.selectGenresList(genres);
        ExcelUtil<Genres> util = new ExcelUtil<Genres>(Genres.class);
        util.exportExcel(response, list, "音乐流派数据");
    }

    /**
     * 获取音乐流派详细信息
     */
    @GetMapping(value = "/{genreId}")
    public AjaxResult getInfo(@PathVariable("genreId") Long genreId)
    {
        return success(genresService.selectGenresByGenreId(genreId));
    }

    /**
     * 新增音乐流派
     */
    @PreAuthorize("@ss.hasPermi('site:genres:add')")
    @Log(title = "音乐流派", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Genres genres)
    {
        return toAjax(genresService.insertGenres(genres));
    }

    /**
     * 修改音乐流派
     */
    @PreAuthorize("@ss.hasPermi('site:genres:edit')")
    @Log(title = "音乐流派", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Genres genres)
    {
        return toAjax(genresService.updateGenres(genres));
    }

    /**
     * 删除音乐流派
     */
    @PreAuthorize("@ss.hasPermi('site:genres:remove')")
    @Log(title = "音乐流派", businessType = BusinessType.DELETE)
	@DeleteMapping("/{genreIds}")
    public AjaxResult remove(@PathVariable Long[] genreIds)
    {
        return toAjax(genresService.deleteGenresByGenreIds(genreIds));
    }

    /**
     * 根据流派ID查询其下的音乐曲目列表
     */
    @GetMapping("/{genreId}/tracks")
    public TableDataInfo getGenresMusic(@PathVariable("genreId") Long genreId)
    {
        startPage();
        // 调用 Service 层方法，传入流派ID进行查询
        List<Tracks> list = genresService.selectTracksByGenreId(genreId);
        return getDataTable(list);
    }
}
